/*
 * Copyright (c) 2010-2020 IISI.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IISI.
 */
package com.iisigroup.log.analysis.rdbms.dao;
 
import bo.ReasonEstimateBo;

import com.iisigroup.log.analysis.model.StuckThreadRecord;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class StackTraceDao  extends TableJDBCDao{
    //================================================
    //== [Enumeration types] Block Start
    //== [Enumeration types] Block End 
    //================================================
    //== [static variables] Block Start
    private final static Logger LOGGER = LoggerFactory.getLogger(StackTraceDao.class);
    //== [static variables] Block Stop 
    //================================================
    //== [instance variables] Block Start
    //== [instance variables] Block Stop 
    //================================================
    //== [static Constructor] Block Start
    //== [static Constructor] Block Stop 
    //================================================
    //== [Constructors] Block Start (含init method)
    //== [Constructors] Block Stop 
    //================================================
    //== [Static Method] Block Start
    //== [Static Method] Block Stop 
    //================================================
    //== [Accessor] Block Start
    //== [Accessor] Block Stop 
    //================================================
    //== [Overrided Method] Block Start (Ex. toString/equals+hashCode)

    public Map<String,Integer> getTackTraceUidMap(){
        final Map<String,Integer> result = new HashMap<String,Integer>();
        final Connection conn = TableJDBCDao.getInstance();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            final String sql = "select UID, STACKTRACE  from STACKTRACE ;  "; 
            final ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                // Retrieve by column name

                final String stacktrace = rs.getString("STACKTRACE");
                final Integer uid = rs.getInt("UID");
                // Display values
                //System.out.print("person_id: " + personId);
                //System.out.println("site_id: " + siteId);
                result.put(stacktrace,uid);
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        
        return result;
    }
    public List<String> getDistinctStucktraceList(boolean onlineOperation) {
        final List<String> result = new ArrayList<String>();
        final Connection conn = TableJDBCDao.getInstance();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            final String sqloffline = "select distinct stacktrace  from stuck where client_ip is null ;  ";
            final String sqlonline = "select distinct stacktrace  from stuck where client_ip is not null ;  ";
            final ResultSet rs = stmt.executeQuery(onlineOperation ?sqlonline:sqloffline);
            while (rs.next()) {
                // Retrieve by column name
                final String stacktrace = rs.getString("stacktrace"); 
                result.add(stacktrace);
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        return result;
    }
    public void insertData(final List<StuckThreadRecord> list){
        final Connection conn = TableJDBCDao.getInstance();
        final  String sqlTemplate = "INSERT INTO STUCK (UID, HOST_IP , DATE , TIME , CLIENT_IP , USER_ID , STACKTRACE ) VALUES (%s ,'%s' ,'%s','%s','%s','%s','%s' ) ;";
        final  String sqlTemplateNull = "INSERT INTO STUCK (UID, HOST_IP , DATE , TIME , CLIENT_IP , USER_ID , STACKTRACE ) VALUES (%s ,'%s' ,'%s','%s', null , null,'%s' ) ;";
        Statement stmt;
        try {
            stmt = conn.createStatement();
            int count =0;
            for(StuckThreadRecord str : list ){
                count++;
                String sql = String.format(sqlTemplate,count , str.getHostIp() , str.getDate() ,str.getTime() , str.getClientIp() ,str.getUserId() ,str.getStackTrace());
                String sql1 = String.format(sqlTemplateNull,count , str.getHostIp() , str.getDate() ,str.getTime() ,   str.getStackTrace());
                if(str.getClientIp() == null){
                    stmt.addBatch(sql1);
                }else{
                    stmt.addBatch(sql);
                }
               
            }
            stmt.executeBatch();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
    } 
    public void insertDataByswlList(final List<String> sqls ){
        final Connection conn = TableJDBCDao.getInstance();
       
        Statement stmt;
        try {
            stmt = conn.createStatement();
            for(String sql:sqls ){
                stmt.addBatch(sql);
            }
            stmt.executeBatch();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
    }
    //== [Overrided Method] Block Stop 
    //================================================
    //== [Method] Block Start
    //####################################################################
    //## [Method] sub-block : 
    //####################################################################    
    //== [Method] Block Stop 
    //================================================
    //== [Inner Class] Block Start
    //== [Inner Class] Block Stop 
    //================================================
}
